const fs = require('fs');
const path = require('path');

// 读取Vue文件
const filePath = path.resolve(__dirname, 'components/agency-info/agency-info.vue');
const content = fs.readFileSync(filePath, 'utf-8');

console.log('===== 检查特殊字符 =====');

// 检查BOM头
if (content.charCodeAt(0) === 0xFEFF) {
  console.error('发现BOM头字符！这可能导致语法错误。');
}

// 检查不可见字符
const specialChars = [];
for (let i = 0; i < content.length; i++) {
  const charCode = content.charCodeAt(i);
  // 检查是否是控制字符（除了换行、回车、制表符）
  if ((charCode < 32 || charCode >= 127) && charCode !== 10 && charCode !== 13 && charCode !== 9) {
    specialChars.push({ position: i, charCode: charCode, line: getLineNumber(content, i) });
  }
}

if (specialChars.length > 0) {
  console.error(`发现 ${specialChars.length} 个特殊字符：`);
  specialChars.forEach(char => {
    console.error(`位置: ${char.position}, 行号: ${char.line}, ASCII: ${char.charCode}`);
  });
} else {
  console.log('未发现特殊字符');
}

// 检查脚本部分的语法结构
const scriptRegex = /<script>([\s\S]*?)<\/script>/;
const scriptMatch = content.match(scriptRegex);
const script = scriptMatch ? scriptMatch[1] : '';

// 按行分割脚本内容
const scriptLines = script.split('\n');

console.log('\n===== 检查脚本行结构 =====');

// 检查空行和注释
let inMultiLineComment = false;

for (let i = 0; i < scriptLines.length; i++) {
  const line = scriptLines[i].trim();
  const originalLine = scriptLines[i];
  
  // 检查多行注释
  if (inMultiLineComment) {
    const endCommentPos = line.indexOf('*/');
    if (endCommentPos !== -1) {
      inMultiLineComment = false;
    }
    continue;
  }
  
  const startCommentPos = line.indexOf('/*');
  if (startCommentPos !== -1) {
    const endCommentPos = line.indexOf('*/');
    if (endCommentPos === -1 || endCommentPos < startCommentPos) {
      inMultiLineComment = true;
    }
    continue;
  }
  
  // 跳过单行注释和空行
  if (line.startsWith('//') || line === '') {
    continue;
  }
  
  // 检查对象属性后面的逗号
  if (line.includes(':') && !line.includes('//') && !line.includes('/*')) {
    // 简单检查对象属性格式
    if (!line.endsWith(',') && 
        !line.includes('{') && 
        !line.includes('}') && 
        !line.endsWith(';') && 
        !line.includes('function') && 
        !line.includes('=>') && 
        !line.includes('if') && 
        !line.includes('for') && 
        !line.includes('while') && 
        !line.includes('switch') && 
        !line.includes('try') && 
        !line.includes('catch')) {
      // 检查下一行是否是新的属性或结束括号
      const nextLineIndex = i + 1;
      if (nextLineIndex < scriptLines.length) {
        const nextLine = scriptLines[nextLineIndex].trim();
        if (nextLine && 
            !nextLine.startsWith('//') && 
            !nextLine.startsWith('/*') && 
            !nextLine.startsWith('}') && 
            !nextLine.startsWith(',')) {
          console.warn(`第 ${i + 1} 行: 可能缺少逗号或存在语法问题`);
          console.log(`该行内容: ${originalLine}`);
        }
      }
    }
  }
}

console.log('\n===== 检查完成 =====');

// 辅助函数：获取字符所在的行号
function getLineNumber(text, position) {
  const lines = text.substring(0, position).split('\n');
  return lines.length;
}